//
//  WHContactsTableView.m
//  AddressBook
//
//  Created by  on 2020/7/28.
//  Copyright © 2020 wh. All rights reserved.
//

#import "WHContactsTableView.h"
#import "WHUser.h"
#import "WHContactsTableViewHeader.h"
static NSString *contactsCellId = @"contactsCellId";
static NSString *contactsHeaderID = @"contactsHeaderID";
@interface WHContactsTableView () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<NSString *> *nameArr;//通讯录姓名数组
@property (nonatomic, strong) NSMutableArray *sectionTitleArr;//索引数组
@property (nonatomic, strong) NSMutableArray *everSectionArrOfnameArr;//数组类型数组 有所少个单元 每个单元有多少名字

@end

@implementation WHContactsTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style]) {
        self.backgroundColor = UIColor.whiteColor;
        self.delegate = self;
        self.dataSource = self;
        self.showsVerticalScrollIndicator = false;
        self.showsHorizontalScrollIndicator = false;
        self.tableFooterView = [[UIView alloc] init];
    }
    return self;
}

- (void)setDataWithNameArr:(NSMutableArray<NSString *> *)nameArr
           sectionTitleArr:(nonnull NSMutableArray *)sectionTitleArr
   everSectionArrOfnameArr:(nonnull NSMutableArray *)everSectionArrOfnameArr
{
    self.nameArr = nameArr;
    self.sectionTitleArr = sectionTitleArr;
    self.everSectionArrOfnameArr = everSectionArrOfnameArr;
}

- (void)removeAllArr
{
    if (self.nameArr) {
        [self.nameArr removeAllObjects ];
    }
    if (self.sectionTitleArr) {
        [self.sectionTitleArr removeAllObjects];
    }
    if (self.everSectionArrOfnameArr) {
        [self.everSectionArrOfnameArr removeAllObjects];
    }
}

#pragma mark - tableView deletate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ((NSArray *)self.everSectionArrOfnameArr[section]).count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.everSectionArrOfnameArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WHContactsTableViewHeader *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:contactsHeaderID];
    if (!headerView) {
        headerView = [[WHContactsTableViewHeader alloc] initWithReuseIdentifier:contactsHeaderID];
    }
    [headerView setTitleWithText:self.sectionTitleArr[section]];
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:contactsCellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contactsCellId];
    }
    NSString * cellText = ((WHUser *)((NSArray *)self.everSectionArrOfnameArr[indexPath.section])[indexPath.row]).name;
    cell.textLabel.text = cellText;
    return cell;
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.sectionTitleArr;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    return self.sectionTitleArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",self.everSectionArrOfnameArr[indexPath.section][indexPath.row]);
    if (self.contactsTableViewDelegate && [self.contactsTableViewDelegate respondsToSelector:@selector(didSelectRowActionWithCellText:)]) {
        [self.contactsTableViewDelegate didSelectRowActionWithCellText:((WHUser *)((NSArray *)self.everSectionArrOfnameArr[indexPath.section])[indexPath.row]).name];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.superview endEditing:YES];
}

#pragma mark - 懒加载
- (NSMutableArray<NSString *> *)nameArr
{
    if (!_nameArr) {
        _nameArr = [[NSMutableArray alloc] init];
    }
    return _nameArr;
}
- (NSMutableArray *)everSectionArrOfnameArr
{
    if (!_everSectionArrOfnameArr) {
        _everSectionArrOfnameArr = [[NSMutableArray alloc] init];
    }
    return _everSectionArrOfnameArr;
}
- (NSMutableArray *)sectionTitleArr
{
    if (!_sectionTitleArr) {
        _sectionTitleArr = [NSMutableArray arrayWithArray:[UILocalizedIndexedCollation currentCollation].sectionTitles];
    }
    return _sectionTitleArr;
}
@end
